﻿using System;
using System.Collections.Generic;
using Relay.Core.Plugins;
using Relay.Core.Configuration;

namespace Relay.Core.Protocol {
    /// <summary>
    /// Provides an application-level abstraction layer for implementing an IRC or IRC-like protocol application.
    /// </summary>
    public class ApplicationService {
        #region Fields
        private readonly List<IPlugin> _plugins = new List<IPlugin>();
        private IInterfacePlugin _interfacePlugin = null;
        private ConfigurationService _config = null;
        #endregion

        #region Events
        /// <summary>
        /// Occurs when the application is ready to close.
        /// </summary>
        public event EventHandler<EventArgs> Closing;
        #endregion

        #region Constructor
        /// <summary>
        /// Initialize an IRC application service object.
        /// </summary>
        public ApplicationService(IInterfacePlugin interfacePlugin) {
            _interfacePlugin = interfacePlugin;

            _interfacePlugin.MainWindowClosed += new EventHandler<EventArgs>(MainWindowClosed);

            _interfacePlugin.Initialize(this);
            _interfacePlugin.DisplayInterface();
        }

        /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// <see cref="ApplicationService"/> is reclaimed by garbage collection.
        /// </summary>
        ~ApplicationService() {
            _interfacePlugin.Terminate();
        }
        #endregion

        #region Private Methods
        private void MainWindowClosed(object sender, EventArgs e) {
            OnClosing(new EventArgs());
        }
        #endregion

        #region Properites
        /// <summary>
        /// Gets the interface plugin used for this application.
        /// </summary>
        /// <value>The interface plugin.</value>
        public IInterfacePlugin InterfacePlugin {
            get { return _interfacePlugin; }
        }

        /// <summary>
        /// Gets or sets the configuration service for this application.
        /// </summary>
        /// <value>The configuration service.</value>
        public ConfigurationService Config {
            get { return _config; }
            internal set { _config = value; }
        }
        /// <summary>
        /// Gets the list of plugins that are loaded by this application.
        /// </summary>
        /// <value>The array of plugins.</value>
        public IPlugin[] Plugins {
            get { return _plugins.ToArray(); }
        }
        #endregion

        #region Protected Methods
        /// <summary>
        /// Raises the <see cref="E:Closing"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected virtual void OnClosing(EventArgs e) {
            if (Closing != null) Closing(this, e);
        }
        #endregion
    }
}
